package com.lepu.app.fun.grow.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.app.utils.PlaySoundUtil;
import com.eyzhs.app.R;

public class GrowSettingMusicAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private String[] mArrayList = null;
	private int mCurrentIndex = 0;
	private ListView mListView;

	public GrowSettingMusicAdapter(Context context, ListView listView,String[] arrayList, int index) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mListView = listView;
		mArrayList = arrayList;
		mCurrentIndex = index;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.length > 0) {
			size = mArrayList.length;
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.grow_setting_music_listitem, null);

			holder = new ViewHolder();
			
			ImageView leftImageView = (ImageView) convertView.findViewById(R.id.leftImageView);
			TextView nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
			ImageView rightImageView = (ImageView) convertView.findViewById(R.id.rightImageView);
			
			holder.leftImageView = leftImageView;
			holder.nameTextView = nameTextView;
			holder.rightImageView = rightImageView;
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		String name = mArrayList[position];
		
		holder.nameTextView.setText(name);
		
		String leftTag = String.format("left_%d", position);
		holder.leftImageView.setTag(leftTag);
		
		String rightTag = String.format("right_%d", position);
		holder.rightImageView.setTag(rightTag);
		if(position == mCurrentIndex)
		{
			holder.rightImageView.setSelected(true);
		}else{
			holder.rightImageView.setSelected(false);
		}
		
		holder.leftImageView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				PlaySoundUtil.getInstance().stop(null);
				boolean selected = v.isSelected();
				if(selected){
					v.setSelected(false);
					return;
				}
				
				for(int i=0;i<mArrayList.length;i++)
				{
					String leftTag = String.format("left_%d", i);
					ImageView leftImageView = (ImageView) mListView.findViewWithTag(leftTag);
					leftImageView.setSelected(false);
				}
				
				v.setSelected(true);
				
				int playResId = R.raw.music0;
				
				switch (position) {
				case 0:
					playResId = R.raw.music0;
					break;
				case 1:
					playResId = R.raw.music1;
					break;
				case 2:
					playResId = R.raw.music2;
					break;

				default:
					break;
				}
				PlaySoundUtil.getInstance().play(mContext,playResId,false, null);
			}
		});

		return convertView;
	}
	
	private static class ViewHolder {
		ImageView leftImageView;
		TextView nameTextView;
		ImageView rightImageView;
	}
}
